﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.RequestSecurityTokenSerializer
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IdentityModel.Tokens;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security.Tokens;
using System.Xml;

namespace Microsoft.InfoCards
{
  internal class RequestSecurityTokenSerializer
  {
    private XmlWriter m_writer;
    private ProtocolProfile m_profile;

    public XmlWriter Writer
    {
      get
      {
        return this.m_writer;
      }
    }

    public ProtocolProfile Profile
    {
      get
      {
        return this.m_profile;
      }
    }

    private XmlNames.IWSTrust WSTrust
    {
      get
      {
        return this.Profile.WSTrust;
      }
    }

    private XmlNames.IWSPolicy WSPolicy
    {
      get
      {
        return this.Profile.WSPolicy;
      }
    }

    private string WSTPrefix
    {
      get
      {
        return this.Profile.WSTrust.DefaultPrefix;
      }
    }

    private string WSIPrefix
    {
      get
      {
        return "wsid";
      }
    }

    private string WSPPrefix
    {
      get
      {
        return this.Profile.WSPolicy.DefaultPrefix;
      }
    }

    private string DSIGPrefix
    {
      get
      {
        return "dsig";
      }
    }

    public RequestSecurityTokenSerializer(XmlWriter writer, ProtocolProfile profile)
    {
      this.m_writer = writer;
      this.m_profile = profile;
    }

    public void WriteClaimElement(string claimUri, bool bIsOptionalClaim)
    {
      this.Writer.WriteStartElement(this.WSIPrefix, "ClaimType", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      this.Writer.WriteAttributeString("Uri", claimUri);
      if (bIsOptionalClaim)
        this.Writer.WriteAttributeString("Optional", "true");
      this.Writer.WriteEndElement();
    }

    public void WriteClaimsElement(
      Dictionary<string, bool> disclosedClaims,
      string[] requiredClaims,
      string[] optionalClaims)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.ClaimsElement, this.WSTrust.Namespace);
      this.Writer.WriteAttributeString(this.WSTrust.Dialect, "http://schemas.xmlsoap.org/ws/2005/05/identity");
      if (disclosedClaims != null)
      {
        foreach (KeyValuePair<string, bool> disclosedClaim in disclosedClaims)
          this.WriteClaimElement(disclosedClaim.Key, disclosedClaim.Value);
      }
      else
      {
        foreach (string requiredClaim in requiredClaims)
          this.WriteClaimElement(requiredClaim, false);
        foreach (string optionalClaim in optionalClaims)
          this.WriteClaimElement(optionalClaim, true);
      }
      this.Writer.WriteEndElement();
    }

    public void WriteRequestTypeElement(string requestType)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.RequestType, this.WSTrust.Namespace);
      this.Writer.WriteString(requestType);
      this.Writer.WriteEndElement();
    }

    public void WritePPIDElement(byte[] ppid)
    {
      this.Writer.WriteStartElement("ClientPseudonym", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      this.Writer.WriteStartElement("PPID", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      this.Writer.WriteBase64(ppid, 0, ppid.Length);
      this.Writer.WriteEndElement();
      this.Writer.WriteEndElement();
    }

    public void WriteAppliesToElement(EndpointAddress address, AddressingVersion version)
    {
      this.Writer.WriteStartElement(this.WSPPrefix, this.WSPolicy.AppliesTo, this.WSPolicy.Namespace);
      address.WriteTo(version, this.Writer);
      this.Writer.WriteEndElement();
    }

    public void WriteKeySizeElement(string keySize)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.KeySize, this.WSTrust.Namespace);
      this.Writer.WriteString(keySize);
      this.Writer.WriteEndElement();
    }

    public void WriteKeyTypeElement(string keyType)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.KeyType, this.WSTrust.Namespace);
      this.Writer.WriteString(keyType);
      this.Writer.WriteEndElement();
    }

    public void WriteUseKeyElement(XmlElement useKey)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.UseKey, this.WSTrust.Namespace);
      this.Writer.WriteStartElement(this.DSIGPrefix, "KeyInfo", "http://www.w3.org/2000/09/xmldsig#");
      this.Writer.WriteStartElement(this.DSIGPrefix, "KeyValue", "http://www.w3.org/2000/09/xmldsig#");
      useKey.WriteContentTo(this.Writer);
      this.Writer.WriteEndElement();
      this.Writer.WriteEndElement();
      this.Writer.WriteEndElement();
    }

    public void WriteBinarySecretElement(BinarySecretSecurityToken bst)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.Entropy, this.WSTrust.Namespace);
      this.Profile.TokenSerializer.WriteToken(this.Writer, (SecurityToken) bst);
      this.Writer.WriteEndElement();
    }

    public void WriteInfoCardReferenceElement(InfoCard card)
    {
      this.Writer.WriteStartElement(this.WSIPrefix, "InformationCardReference", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      this.Writer.WriteStartElement(this.WSIPrefix, "CardId", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      this.Writer.WriteString(card.Id.ToString());
      this.Writer.WriteEndElement();
      this.Writer.WriteStartElement(this.WSIPrefix, "CardVersion", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      this.Writer.WriteString(card.Epoch.ToString((IFormatProvider) CultureInfo.InvariantCulture));
      this.Writer.WriteEndElement();
      this.Writer.WriteEndElement();
    }

    public void WriteDisplayTokenElement(CultureInfo displayCulture)
    {
      this.Writer.WriteStartElement(this.WSIPrefix, "RequestDisplayToken", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      this.Writer.WriteAttributeString("xml", "lang", (string) null, displayCulture.TwoLetterISOLanguageName);
      this.Writer.WriteEndElement();
    }

    public void WriteTokenTypeElement(string tokenType)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.TokenType, this.WSTrust.Namespace);
      this.Writer.WriteString(tokenType);
      this.Writer.WriteEndElement();
    }

    public void WriteEncryptionAlgorithmElement(string encryptionAlgorithm)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.EncryptionAlgorithm, this.WSTrust.Namespace);
      this.Writer.WriteString(encryptionAlgorithm);
      this.Writer.WriteEndElement();
    }

    public void WriteSignWithElement(string signWith)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.SignWith, this.WSTrust.Namespace);
      this.Writer.WriteString(signWith);
      this.Writer.WriteEndElement();
    }

    public void WriteEncryptWithElement(string encryptWith)
    {
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.EncryptWith, this.WSTrust.Namespace);
      this.Writer.WriteString(encryptWith);
      this.Writer.WriteEndElement();
    }

    public void WriteKeyWrapAlgorithmElement(string keyWrapAlgorithm)
    {
      InfoCardTrace.Assert(this.WSTrust.Version == XmlNames.WSSpecificationVersion.WSTrustOasis2007, "KeyWrapAlgorithm element is valid in WS-Trust 1.3 only");
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.KeyWrapAlgorithm, this.WSTrust.Namespace);
      this.Writer.WriteString(keyWrapAlgorithm);
      this.Writer.WriteEndElement();
    }

    public void WriteUnprocessedPolicyElements(MemoryStream unprocessedElements)
    {
      unprocessedElements.Seek(0L, SeekOrigin.Begin);
      XmlReader reader = XmlReader.Create((Stream) unprocessedElements, new XmlReaderSettings()
      {
        ConformanceLevel = ConformanceLevel.Fragment
      });
      while (!reader.EOF)
        this.Writer.WriteNode(reader, true);
    }

    public void WriteSecondaryParametersElement(string policyXml)
    {
      InfoCardTrace.Assert(this.WSTrust.Version == XmlNames.WSSpecificationVersion.WSTrustOasis2007, "SecondaryParameters element is valid in WS-Trust 1.3 only");
      this.Writer.WriteStartElement(this.WSTPrefix, this.WSTrust.SecondaryParameters, this.WSTrust.Namespace);
      XmlReader reader = XmlReader.Create((TextReader) new StringReader(policyXml), new XmlReaderSettings()
      {
        ConformanceLevel = ConformanceLevel.Fragment
      });
      while (!reader.EOF)
        this.Writer.WriteNode(reader, true);
      this.Writer.WriteEndElement();
    }
  }
}
